import Vue from 'vue';
import VueRouter from 'vue-router';
import Home from '../views/Home.vue';
import mechanismInfoAddOrUpdate from '../views/mechanismInfoAddOrUpdate.vue';
import mechanismContactInfo from '../views/mechanismContactInfo.vue';

Vue.use(VueRouter); //应用路由插件

//创建路由route，数组形式。
const routes = [
  //
  {
    path: '/',
    name: 'Home',
    component: Home,
    meta: {
      title: '机构信息建档管理系统',
      keepAlive: true, // 需要被缓存
    },
    children: [
      {
        path: '/baseInfo',
        name: 'BaseInfo',
        meta: {
          title: '机构基本信息',
          keepAlive: true,
        },
        component: mechanismInfoAddOrUpdate, //普通加载，需要import路由组件
      },
      {
        path: '/contactInfo',
        name: 'ContactInfo',
        meta: {
          title: '机构联系方式信息',
          keepAlive: true,
        },
        component: mechanismContactInfo, //普通加载，需要import路由组件
      },
    ],
  },
];
//创建全局唯一的路由器router
const router = new VueRouter({
  mode: 'history', //路由的 history 模式
  base: process.env.BASE_URL, //打包路径，默认为/，可以修改
  routes, //路由数组
});

router.beforeEach((to, from, next) => {
  //路由发生改变时,触发 --- 用于title的设置
  window.document.title =
    to.meta.title == undefined ? '等待响应' : to.meta.title;

  next();
});

export default router; //暴露出去
